package SystemB;

/******************************************************************************************************************
* File:SecurityAlarmSensor.java
* Course: 17655
* Project: Assignment A3
* Copyright: Copyright (c) 2013 Carnegie Mellon University
* Versions:
*	1.0 March 2013 - subbu
*
******************************************************************************************************************/
import EventPackage.*;
import TermioPackage.Termio;

class SecurityAlarmSensor
{
	public static void main(String args[])
	{
		String EvtMgrIP;					// Event Manager IP address
		EventManagerInterface em = null;	// Interface object to the event manager
		boolean Done = false;				// Loop termination flag
		Termio UserInput = new Termio();     //termio package
		String Option = null;				// Menu choice from user

		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////

 		if ( args.length == 0 )
 		{
			// event manager is on the local system

			System.out.println("\n\nAttempting to register on the local machine..." );

			try
			{
				// Here we create an event manager interface object. This assumes
				// that the event manager is on the local machine

				em = new EventManagerInterface();
			}

			catch (Exception e)
			{
				System.out.println("Error instantiating event manager interface: " + e);

			} // catch

		} else {

			// event manager is not on the local system

			EvtMgrIP = args[0];

			System.out.println("\n\nAttempting to register on the machine:: " + EvtMgrIP );

			try
			{
				// Here we create an event manager interface object. This assumes
				// that the event manager is NOT on the local machine

				em = new EventManagerInterface( EvtMgrIP );
				
			}

			catch (Exception e)
			{
				System.out.println("Error instantiating event manager interface: " + e);

			} // catch

		} // if

 		//get the registered id and time for this simulator and print to console
 		
 		try
    	{
			System.out.println("   Participant id: " + em.GetMyId() );
			System.out.println("   Registration Time: " + em.GetRegistrationTime() + "\n");

		} // try

    	catch (Exception e)
		{
    		System.out.println("Error:: " + e);

		} // catch

		System.out.println("\nInitializing Security Simulation::" );


		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (em != null)
		{
			System.out.println( "\n\n\n\n" );
			System.out.println( "Security Alert Generation Console: \n" );

		
			while (!Done)
			{
				//This is different than how the humidity sensor and temperature sensor simulators work. This one will request a user input for alert
				System.out.println( "\n\n\n\nSelect an Option: \n" );
				System.out.println( "1: Generate Window break alert" );
				System.out.println( "2: Generate Door break alert" );
				System.out.println( "3: Generate Motion detection alert" );
				System.out.println( "4: Reset Alerts" );
				System.out.println( "X: Stop System\n" );
				System.out.print( "\n>>>> " );
				Option = UserInput.KeyboardReadString();
				


				if ( Option.equals( "1" ) ) //for the window break alert
				{
					//post a security message with window break alert
					PostSecurityAlert (em, "WB");

				} // if

				//////////// option 2 ////////////

				if ( Option.equals( "2" ) ) //door break alert
				{
					//post a security message with door break alert
					PostSecurityAlert (em, "DB");
					
				} // if
				
				if ( Option.equals( "3" ) ) //motion detection alert
				{
					//post a security message with motion detection alert
					PostSecurityAlert (em, "MD");
					
				} // if
				
				if ( Option.equals( "4" )) //reset alerts	
				{
					//post a security message to reset alerts
					PostSecurityAlert (em, "RST");
					
				} // if

				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
									
					Done = true;
					System.out.println( "\nSecurity Sensor Console Stopped... Exit monitor mindow to return to command prompt." );
					
                                        //now shutting down the whole system - sending event 99
                                        System.out.println( "***HALT MESSAGE RECEIVED - SHUTTING DOWN SYSTEM***" );

                                        // Here we create the stop event.
                                        Event evt;
                                        evt = new Event( (int) 99, "XXX" );

                                        // Here we send the event to the event manager.
                                        try
                                        {
                                           em.SendEvent( evt );
                                        } // try
                                        catch (Exception e)
                                        {
                                           System.out.println("Error sending halt message:: " + e);
                                        } // catch
                                       
				} // if

			} // while

		} else {

			System.out.println("Unable to register with the event manager.\n\n" );

		} // if

	} // main


	/***************************************************************************
	* CONCRETE METHOD:: PostSecurityAlert
	* Purpose: This method posts an event with ID 6 to indicate a security alert. 
	* The message of the event will contain the type f security alert
	* Arguments: EventManagerInterface ei - this is the eventmanger interface
	*			 where the event will be posted.
	*
	*			 String SecurityAlert- This indicates what type of security alert it is
	*
	* Returns: none
	*
	* Exceptions: None
	*
	***************************************************************************/

	static private void PostSecurityAlert(EventManagerInterface ei, String SecurityAlert )
	{
		// Here we create the event.

		Event evt = new Event( (int) 6, SecurityAlert );

		// Here we send the event to the event manager.

		try
		{
			ei.SendEvent( evt );
			

		} // try

		catch (Exception e)
		{
			System.out.println( "Error Posting Security Alert:: " + e );

		} // catch

	} // PostSecurityAlert

} // Security Sensor